package com.haredot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haredot.entity.ResourceType;
import com.haredot.service.ResourceTypeService;
import com.haredot.mapper.ResourceTypeMapper;
import com.haredot.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.Objects;

/**
* @author Administrator
* @description 针对表【tb_resource_type】的数据库操作Service实现
* @createDate 2023-09-02 19:39:38
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class ResourceTypeServiceImpl extends ServiceImpl<ResourceTypeMapper, ResourceType> implements ResourceTypeService{

    @Override
    public R<?> saveResourceType(ResourceType resourceType) {
        // 根据资源类型名、查询资源类型信息
        Long aLong = this.getBaseMapper().selectCount(new LambdaQueryWrapper<ResourceType>().eq(ResourceType::getName, resourceType.getName()));

        if (aLong != 0)  return R.fail(R.StatusCode.BAD).data(Map.of("name", "资源类型名已存在"));
        // 保存数据
        return this.save(resourceType) ? R.ok().build() : R.fail().build() ;
    }

    @Override
    public R<?> modifyResourceType(ResourceType resourceType) {
        // 根据资源类型名、查询资源类型信息
        ResourceType type = this.getBaseMapper().selectOne(new LambdaQueryWrapper<ResourceType>().eq(ResourceType::getName, resourceType.getName()));

        if (type !=null && !Objects.equals(resourceType.getName(), type.getName()))  return R.fail(R.StatusCode.BAD).data(Map.of("name", "资源类型名已存在"));
        // 修改数据
        return this.updateById(resourceType) ? R.ok().build() : R.fail().build() ;
    }

    @Override
    public R<?> removeResourceType(Long id) {
        int len = this.getBaseMapper().deleteResourceType(id);

        return len > 0 ? R.ok().build() :  R.fail().build();
    }
}




